package com.hsq.androidproject.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.db.DBHelper;
import com.hsq.androidproject.entity.bd_entity.TopicsLike;
import com.hsq.androidproject.entity.find.FindMineTL;
import com.hsq.androidproject.ui.ImageActivity;
import com.hsq.androidproject.ui.find_fragment.taolun.ShowTopicsOneActivity;
import com.hsq.androidproject.utils.glide.GlideCircleTransform;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * Created by lenovo on 2016/11/22.
 */

public class MyListViewFindTopicsAdapter extends BaseAdapter {        ////热门讨论区
    private List<FindMineTL.TopicsBean> mData;
    private Context context;
    private String title;
    private DBHelper dbHelper;
    private String userName = "peng";

    public MyListViewFindTopicsAdapter(List<FindMineTL.TopicsBean> mData, String title, Context context) {
        this.mData = mData;
        this.context = context;
        this.title = title;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_findtopics_listview, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);
            viewHolder.ll_layout = (LinearLayout) convertView.findViewById(R.id.ll_layout);
            viewHolder.tv_userNick = (TextView) convertView.findViewById(R.id.tv_userNick);
            viewHolder.tv_likeCountt = (TextView) convertView.findViewById(R.id.tv_likeCountt);
            viewHolder.tv_replyCount = (TextView) convertView.findViewById(R.id.tv_replyCount);
            viewHolder.tv_lastTopicModify = (TextView) convertView.findViewById(R.id.tv_lastTopicModify);
            viewHolder.gv_images = (GridView) convertView.findViewById(R.id.gv_images);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final FindMineTL.TopicsBean topicsBean = mData.get(position);
        final String thumb = topicsBean.getUserAvatar();
        Glide.with(context)
                .load(thumb)
                .transform(new GlideCircleTransform(context))//圆形
                .into(viewHolder.iv_img);

        final String userNick = topicsBean.getUserNick();
        viewHolder.tv_userNick.setText(userNick);

        int likeCountt = topicsBean.getLikeCount();
        dbHelper = new DBHelper(context);
        TopicsLike topicsLike = new TopicsLike(viewHolder.tv_title.getText().toString(), userName);
        if (dbHelper.isExistT(topicsLike)) {
            int count = likeCountt + 1;
            viewHolder.tv_likeCountt.setText(count + "");
        } else {
            viewHolder.tv_likeCountt.setText(likeCountt + "");
        }


        int replyCount = topicsBean.getReplyCount();
        viewHolder.tv_replyCount.setText(replyCount + "");

        long postTime = topicsBean.getLastReplyTime();          ///////postTime 应该是:lastReplyTime
        String time = getTimeFromMillisecond(postTime);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String date = sdf.format(new Date());

        long diff = 0;
        long hours = 0;
        long day = 0;
        long minutes = 0;
        try {
            Date d1 = sdf.parse(date);
            Date d2 = sdf.parse(time);
            diff = d1.getTime() - d2.getTime();
            long days = diff / (1000 * 60 * 60 * 24);
            day = days;
            hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (hours > 0) {///hours + ":" + minutes
            viewHolder.tv_lastTopicModify.setText(hours + "小时前");
        } else if (hours == 0) {
            if (minutes > 1) {
                viewHolder.tv_lastTopicModify.setText(minutes + "分钟前");
            } else {
                viewHolder.tv_lastTopicModify.setText("刚刚");
            }
        } else if (hours < 0 && hours > -23) {
            viewHolder.tv_lastTopicModify.setText("昨天");
        } else {
            viewHolder.tv_lastTopicModify.setText(day + "天前");
        }

        //////处理图片
        final List<FindMineTL.TopicsBean.PhotoesBean> pic_urls = topicsBean.getPhotoes();
        try {
            if (pic_urls.size() == 1) {         //单图
                //单图
                //显示布局
                viewHolder.iv_image.setVisibility(View.VISIBLE);
                viewHolder.gv_images.setVisibility(View.GONE);
                Glide.with(context)
                        .load(pic_urls.get(0).getThumb())
                        .into(viewHolder.iv_image);
            } else if (pic_urls.size() == 0) {      //没图
                viewHolder.iv_image.setVisibility(View.GONE);
                viewHolder.gv_images.setVisibility(View.GONE);
            } else if (pic_urls.size() > 1) {            //多图
                viewHolder.iv_image.setVisibility(View.GONE);
                viewHolder.gv_images.setVisibility(View.VISIBLE);
                //ListView 某一个item中显示多图gridview
                //适配器 ListView 嵌套GridView
                viewHolder.gv_images.setAdapter(new GridViewImgsAdapter(context, pic_urls));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        viewHolder.iv_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ////////大图
                String bigImg = pic_urls.get(0).getImage();
                Intent intent = new Intent(context, ImageActivity.class);
                intent.putExtra("bigImg", bigImg);
                context.startActivity(intent);
            }
        });
        viewHolder.tv_title.setText(topicsBean.getTitle());
        viewHolder.tv_content.setText(topicsBean.getContent());

        viewHolder.ll_layout.setOnClickListener(new View.OnClickListener() {        ///点击跳转页面
            @Override
            public void onClick(View v) {
                int tid = topicsBean.getTid();
                Intent intent = new Intent(context, ShowTopicsOneActivity.class);        ////展示一个人的信息
                intent.putExtra("tid", tid);
                intent.putExtra("title", title);
                intent.putExtra("commentCount", topicsBean.getReplyCount());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public static String getTimeFromMillisecond(Long millisecond) {		/*毫秒转换为时间*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date(millisecond);
        String timeStr = simpleDateFormat.format(date);
        return timeStr;
    }


    class ViewHolder {
        public ImageView iv_img;        //头像
        private TextView tv_userNick, tv_likeCountt, tv_replyCount, tv_lastTopicModify; //// 姓名 喜欢 评论 时间
        private LinearLayout ll_layout;
        private GridView gv_images;
        private ImageView iv_image;
        private TextView tv_title, tv_content;       //////内容标题 内容内容
    }


}
